package com.sxpi.common.validator.idcard;


import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @Author: 董伟豪
 * @Date: 2022/10/22 15:27
 */
public class IdcardValidator implements ConstraintValidator<IdCard, String> {

    @Override
    public void initialize(final IdCard constraintAnnotation) {}

    /**
     * 如果是 true 就通过， false代表不合法，之间报错message
     * @param card
     * @param context
     * @return
     */
    @Override
    public boolean isValid(final String card, final ConstraintValidatorContext context) {
        if(StringUtils.isEmpty(card)){
            return true;
        }
        return validateIdCard(card);
    }

    private boolean validateIdCard(final String idcard) {
        return IdCardUtil.isIdcard(idcard);
    }
}
